#import "FiguresGame.h"
#import "ImageGameComponent.h"

const NSInteger kSizeOfPicture = 90;

@interface FiguresGame ()

@property BOOL isGameOver;
@property NSInteger movement1;
@property NSInteger movement2;

@end

@implementation FiguresGame

-(instancetype) init {
    self = [super init];
    if(self) {
        self.name = @"Figures Game";
        self.type = observation;
        self.isGameOver = NO;
        self.ruleOfTheGame = @"Hit if colored shapes collide!";
        self.movement1 = 1;
        self.movement2 = -1;
    }
    return self;
}

-(void) draw {
    NSInteger i1 = arc4random_uniform(3);
    NSInteger i2 = arc4random_uniform(3);
    NSString* im1 = [NSString stringWithFormat:@"FiguresGame%d",i1+2];
    NSString* im2 = [NSString stringWithFormat:@"FiguresGame%d",i2+2];
    NSInteger imageSize=(kScreenWidth-kHeighMargin)/4;
    NSInteger y = (kScreenHeight - 2*kButtonHeigh)/2 - imageSize/2;
    ImageGameComponent* image1 = [[ImageGameComponent alloc] initWithName:im1 andFrame:CGRectMake(0, y, imageSize, imageSize) andTrans:0];
    ImageGameComponent* image2 = [[ImageGameComponent alloc] initWithName:im2 andFrame:CGRectMake(kScreenWidth - imageSize, y, imageSize, imageSize) andTrans:0];
    [self addComponents:image1];
    [self addComponents:image2];
    NSInteger wallWidth = kScreenWidth / 5;
    ImageGameComponent* wall = [[ImageGameComponent alloc] initWithName:@"wall" andFrame:CGRectMake(kScreenWidth/2 - wallWidth/2, 0, wallWidth, kScreenHeight - 2*kButtonHeigh) andTrans:0];
    [self addComponents:wall];
}

-(void) play {
    [self draw];
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 0.02 target: self selector:@selector(move)userInfo: nil repeats:YES];
        if(self.isGameOver == YES)
           [timer invalidate];
    
}

-(void) move {
    NSInteger x0 = ((ImageGameComponent*)self.components[0]).frame.origin.x;
    NSInteger y0 = ((ImageGameComponent*)self.components[0]).frame.origin.y;
    NSInteger x1 = ((ImageGameComponent*)self.components[1]).frame.origin.x;
    NSInteger y1 = ((ImageGameComponent*)self.components[1]).frame.origin.y;
    NSInteger imageSize=(kScreenWidth-kHeighMargin)/4;
    if(x0 == kScreenWidth) {
        self.movement1 *= -1;
        self.movement2 *= -1;
    }
    [self.components[0] setFrame:CGRectMake(x0+self.movement1, y0, imageSize, imageSize)];
    [self.components[1] setFrame:CGRectMake(x1+self.movement2, y1, imageSize, imageSize)];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SomeGames" object:self userInfo:nil];
    
    if(self.isGameOver)
        self.movement1 = self.movement2 = 0;
}

-(BOOL) isRight {
    NSInteger x0 = ((ImageGameComponent*)self.components[0]).frame.origin.x;
    NSInteger x1 = ((ImageGameComponent*)self.components[1]).frame.origin.x;
    return (abs)((int)(x0 - x1)) <= ((ImageGameComponent*)self.components[0]).frame.size.width ;
}

-(void) gameOver {
    [self.components removeObject:self.components[2]];
    self.isGameOver = YES;
    
}

@end
